import {AccountList, AccountRemove, Accountshow} from "@/common/customs/setting";
import {UploadApi} from "@/configs/controller";
import {decodeAesString} from "@/common/crypto";

export const titleList = [
  {
    title: "基础设置",
    type: 1
  },
  {
    title: "监控指标",
    type: 2
  },
  {
    title: "cpu",
    type: 3
  },
  {
    title: "内存",
    type: 4
  },
  {
    title: "磁盘",
    type: 5
  },
  {
    title: "磁盘空间",
    type: 6
  },

];
export const timeSet = [
  {
    index: 1,
    type: "block",
    switchKey: "is_yc",
    title: "异常人员告警",
    items: [
      {
        type: "input",
        title: "规则说明",
        placeholder: "请输入告警规则",
        key: "ycgz",
      },
    ],
  },
  {
    type: "line"
  },
  {
    index: 2,
    type: "block",
    switchKey: "is_sw",
    title: "上网行为告警",
    items: [
      {
        type: "input",
        title: "规则说明",
        placeholder: "请输入告警规则",
        key: "swgz",
      },
      {
        type: "number",
        title: "参数时间（分钟）",
        placeholder: "参数时间（分钟）",
        key: "swsj",
      },
    ],
  },
  {
    type: "line"
  },
  {
    index: 3,
    type: "block",
    switchKey: "is_sl",
    title: "离校失联告警",
    items: [
      {
        type: "input",
        title: "规则说明",
        placeholder: "请输入告警规则",
        key: "slgz",
      },
      {
        type: "number",
        title: "参数时间（分钟）",
        placeholder: "参数时间（分钟）",
        key: "slsj",
      },
      {
        type: "number",
        title: "抓拍失联时间(分钟)",
        placeholder: "抓拍失联时间(分钟)",
        key: "zpslls",
      },
    ],
  },
  {
    type: "line"
  },
  {
    index: 4,
    type: "block",
    switchKey: "is_wg",
    title: "晚归告警",
    items: [
      {
        type: "input",
        title: "规则说明",
        placeholder: "请输入告警规则",
        key: "latereturngz",
      },
      {
        type: "TimePick",
        title: "正常考勤时间",
        key: "yg",
        change: (event, timeData) => {
          timeData.wg[0] = event[1];
        }
      },
      {
        type: "TimePick",
        title: "晚归考勤时间",
        key: "wg",
        change: (event, timeData) => {
          timeData.weig[0] = event[1];
        },
      },
      {
        type: "TimePick",
        title: "未归考勤时间",
        key: "weig",
        disabled: true,
        // switchKey: "is_wg"
      },
    ]
  },
  {
    type: "line"
  },
  {
    index: 5,
    type: "block",
    switchKey: "is_tt",
    title: "楼顶逗留告警",
    items: [
      {
        type: "input",
        title: "规则说明",
        placeholder: "请输入告警规则",
        key: "ttgz",
      },
      {
        type: "number",
        title: "参数时间（分钟）",
        placeholder: "参数时间（分钟）",
        key: "ttsj",
      },
      {
        type: "TimePick",
        title: "触发时间",
        placeholder: "触发时间",
        key: "tt",
      },

    ]
  },
];
export const carCol = [
  {
    index: 1,
    title: "车位数",
    items: [
      {
        type: "number",
        title: "规则说明",
        placeholder: "请输入车位数",
        key: "zcw",
      },
    ],
  },
  {
    type: "line"
  },
  {
    index: 2,
    title: "校准值",
    items: [
      {
        type: "number",
        title: "规则说明",
        placeholder: "请输入校准值",
        key: "xzz",
      },
    ],
  },
  {
    type: "info",
    key:'sj'
  },
]

export const timeData = [
  {
    type: "number",
    title: "天台告警触发时间（分钟）",
    key: "ttsj",
  },
  {
    type: "number",
    title: "失联告警时间设置（分钟）",
    key: "slsj",
  },
  {
    type: "time",
    title: "考勤结束开始时间",
    key: "jskssj",
  },
  {
    type: "time",
    title: "考勤结束时间",
    key: "jssj",
  },
];
export const editVideo = (item) => {
  let input = document.createElement("input");
  input.style = "display: none";
  input.setAttribute("type", "file");
  input.setAttribute("accept", ".mp4");
  document.body.appendChild(input);
  input.addEventListener("change", async (file) => {
    let imgFile = file.target.files[0];
    let imgBlob = URL.createObjectURL(imgFile);
    const formData = new FormData();
    formData.append("file", file.target.files[0]);
    const api = new UploadApi();
    api.data(formData);
    await api.send();
    const {data} = await api.getDatas();
    item.newVideo = data;
    item.VideoBlob = imgBlob;
  });
  input.click();
};
export const removeVideo = (item) => {
  item.newVideo = "";
  item.VideoBlob = "";
  item.sp = "";
};
export const removeImg = (item) => {
  item.newImgUrl = "";
  item.imgBlob = "";
  item.img = "";
};
export const editImg = (item) => {
  let input = document.createElement("input");
  input.style = "display: none";
  input.setAttribute("type", "file");
  input.setAttribute("accept", "image/png, image/gif, image/jpeg");
  document.body.appendChild(input);
  input.addEventListener("change", async (file) => {
    let imgFile = file.target.files[0];
    let imgBlob = URL.createObjectURL(imgFile);
    const formData = new FormData();
    formData.append("file", file.target.files[0]);
    const api = new UploadApi();
    api.data(formData);
    await api.send();
    const {data} = await api.getDatas();
    item.newImgUrl = data;
    item.imgBlob = imgBlob;
  });
  input.click();
};

export const rule = {
  name: [{
    required: true,
    message: "请输入",
    trigger: "change",
  }],
  username: [{
    required: true,
    message: "请输入",
    trigger: "change",
  }],
  password: [{
    required: true,
    message: "请输入",
    trigger: "change",
  }],
  roles_id: [{
    required: true,
    message: "请选择",
    trigger: "change",
  }],
};

export const addTableConfig = (MyTable, MyForm, Self) => {
  MyTable.columns = [
    {
      title: "序号",
      key: "id",
      width: 100,
      slot: "c-index",
    },
    {
      title: "账号",
      key: "username",
      // width: 180,
    },
    {
      title: "姓名",
      key: "name",
      // width: 100,
      slot: "c-text",
    },
    {
      title: "权限",
      key: "roles_id",
      width: 150,
      slot: "c-roles",
      option: {
        0: "超级管理员",
        1: "值班老师",
      }
    },
    {
      title: "操作",
      key: "syms",
      // width: 80,
      slot: "c-action",
      items: [
        {
          name: "修改",
          color: "",
          callback: async (record) => {
            MyForm.formDatas = {};
            const api = new Accountshow();
            api.params({
              id: record.id
            });
            await api.send();
            const {data} = await api.getDatas();
            MyForm.formDatas = data;
            MyForm.formDatas.password = "";
            MyForm.formDatas.rePassword = "";
            MyForm.formDatas.mobile = decodeAesString(MyForm.formDatas.mobile);
            Self.typeChange(4);
          }
        },
        {
          name: "删除",
          color: "red",
          callback: async (record) => {
            const api = new AccountRemove();
            api.data({
              id: record.id
            });
            api.hint("删除");
            await api.send();
            const data = await api.getDatas();
            MyTable.getDatas();
          }
        },
      ]
    },
  ];
  MyTable.overWidth = 800;
  MyTable.pageSize = 12;
  MyTable.getDatas = async (searchVal) => {
    try {
      const api = new AccountList()
        .params({
          page: MyTable.pageConfig.current,
          page_size: MyTable.pageConfig.pageSize,
          name: searchVal,
        });
      await api.send();
      const {data} = await api.getDatas();
      await MyTable.setDatas(data.data, data.total);
    } catch (err) {
      console.log(err);
    }
  };
  MyTable.onSearch = (val) => {
    MyTable.getDatas(val);
  };
};
